<?php

namespace App\Services;

use EasyWeChat\MiniApp\Application;

class WechatService
{
    protected $app;

    public function __construct()
    {
        $app_id = config('services.weixin.client_id');

        $secret = config('services.weixin.client_secret');

        $config = [
            'app_id' => $app_id,
            'secret' => $secret,
            'token'  => 'easywechat',
            'http'   => [
                'throw'   => config('app.debug'),
                'timeout' => 5.0,
                'retry'   => true
            ],
        ];

        $this->app = new Application($config);
    }


    /**
     * 根据 openid 查找或创建小程序用户
     */
    public function getUserInfo(string $code) : array
    {
        $utils = $this->app->getUtils();

        $session = $utils->codeToSession($code);

        return $session;
    }
}
